/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.product.service.impl.ProductSupplierApproveServiceImpl
   Module Description   :

   Date Created      : 2010/8/18
   Original Author   : feeling
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.product.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;

import com.yaodian100.core.admin.entity.User;
import com.yaodian100.core.admin.entity.UserPermission;
import com.yaodian100.core.admin.entity.Permission.Type;
import com.yaodian100.core.admin.service.UserService;
import com.yaodian100.core.admin.utils.AdminHelper;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.dao.impl.CriteriaInRlike;
import com.yaodian100.core.common.exception.CoreException;
import com.yaodian100.core.common.service.impl.DomainServiceImpl;
import com.yaodian100.ec.product.entity.ProductSupplierApprove;
import com.yaodian100.ec.product.service.ProductService;
import com.yaodian100.ec.product.service.ProductSupplierApproveService;
import com.yaodian100.ec.shelf.service.ShelfService;

/**
 * @author feeling
 * 
 */
public class ProductSupplierApproveServiceImpl extends DomainServiceImpl<ProductSupplierApprove> implements
		ProductSupplierApproveService {
	@Resource
	private ShelfService shelfService;
	@Resource
	private ProductService productService;
	@Autowired
	private UserService userService;
	private final String STRING_SYMBOL = "/";

	public ShelfService getShelfService() {
		return shelfService;
	}

	public void setShelfService(ShelfService shelfService) {
		this.shelfService = shelfService;
	}

	public ProductService getProductService() {
		return productService;
	}

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	@Override
	public List<ProductSupplierApprove> getList(int firstResult, int maxResults, CommonCriteria criteria,
			String[] sortOrder) throws CoreException {
		CriteriaInRlike critInRlike = mergeCritInRlike(criteria, "product.category.categoryPath");
		return super.getList(firstResult, maxResults, critInRlike, sortOrder);
	}

	@Override
	public Number getListSize(CommonCriteria criteria) throws CoreException {
		CriteriaInRlike critInRlike = mergeCritInRlike(criteria, "product.category.categoryPath");
		return super.getListSize(critInRlike);
	}

	/**
	 * 分線維護權限檢核
	 * 
	 * @param commcriteria
	 * @return
	 */
	CriteriaInRlike mergeCritInRlike(CommonCriteria criteria, String tableParam) {
		CriteriaInRlike critInRlike = null;
		User user = AdminHelper.getUser();
		if (null != user) {
			if (null == criteria) {
				criteria = new CommonCriteria();
			}
			critInRlike = new CriteriaInRlike(criteria);
			// 若 user 是 PM部門, 檢核 product.category 的 orgnizationPath 是否合乎規定
			List<Serializable> prs = new ArrayList<Serializable>();
			for (UserPermission up : user.getPermissions(Type.CATEGORY).values()) {
				prs.add(up.getPermissionCode() + STRING_SYMBOL);
			}
			critInRlike.addInRlike(tableParam, prs);
		} else {
			if (null == criteria) {
				criteria = new CommonCriteria();
			}
			critInRlike = new CriteriaInRlike(criteria);
		}
		return critInRlike;
	}
}
